/*
 *  CS202 - Spring 2009">
 *  Professor: Dr. Michael Peterson">
 *  Student: Deylo Woo (dwoo711@gmail.com) ">
 */

package p4startercode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonTest extends JFrame {

    private JButton button;
    private int numClicks = 0;
    private JPanel contentPane;
    private JPanel buttonPanel;
    private boolean buttonState = false;
    private ClownPause pauser;

        /** Creates a new instance of ButtonTest */
    public ButtonTest() {
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      //contentPane = (JPanel) this.getContentPane ();
      button = new JButton ();
      button.setIcon(new ImageIcon( "images/t1.jpg" ));
      button.setPreferredSize(new Dimension(40,40));
      buttonPanel = new JPanel();
      buttonPanel.setLayout(new FlowLayout());
      buttonPanel.add(button);
      add(buttonPanel);
      //setSize (50,50);
      button.addActionListener(new ButtonHandler());
      pauser = new ClownPause();

      center(this);
      pack();
      setVisible (true);
    }

    // a helper routine to center a frame on the screen (will cause problems if
    // frame is bigger than the screen!)
    public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w/2, y = center.y - h/2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }

        /*
     * Add an inner actionListener class!
     */
    class ButtonHandler implements ActionListener{

        public void actionPerformed (ActionEvent event) {

            // if pauser is in paused state, do nothing (ignore button click)
            if(pauser.isPaused())
                return;

            // display the clown
            button.setIcon(new ImageIcon( "images/c1.jpg" ));

            // start a one-second long pause
            pauser.startPause(2000,button,"images/t1.jpg");
            
            


        } // end method actionPerformed

    } // end ButtonHandler inner class

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ButtonTest test = new ButtonTest();
    }

}
